<template>
  <div ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import BetterScroll from 'better-scroll'
  export default {
    name: "Scroll",
    props:{
      propsType:{
        type:Number,
        default:0
      },
      pullUpLoad:{
        type:Boolean,
        default: false
      }
    },
    data(){
      return{
        scroll:null
      }
    },
    mounted() {

      // 创建BScroll对象
      // 建议不直接使用querySelector拿元素
      this.scroll=new BetterScroll(this.$refs.wrapper,{
        click:true,
        observeDOM:true,
        probeType:this.propsType,
        pullUpLoad: this.pullUpLoad
      })
      // this.scroll.refresh()
      // 监听滚动位置
      this.scroll.on('scroll',(position)=>{
        this.$emit('scrollPosition',position);
      })
      // 监听滚动到底部
     if(this.pullUpLoad){
       this.scroll.on('pullingUp',()=>{
         // console.log('监听到底部')
         this.$emit('pullUp');
       })
     }
    },
    methods:{
      scrollTo(x,y,time){
        this.scroll.scrollTo(0, 0, time)
      },
      finishPullUp(){
        this.scroll && this.scroll.finishPullUp();
      },
      refresh(){
        // console.log('------')
        this.scroll && this.scroll.refresh();
      }
    }
  }
</script>

<style scoped>

</style>
